local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());

--概述：控制怪物的动画播放

----------

local animationIdList={
    idle1= "http://www.roblox.com/asset/?id=507766666",
    idle2= "http://www.roblox.com/asset/?id=507766388",
    walk= "http://www.roblox.com/asset/?id=507777826",
    run="http://www.roblox.com/asset/?id=507767714",
}

local loop={
    idle1=false,
    idle2=false,
    walk=true,
    run=true
}

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.player=self.parent.player
	self.character=self.parent


    self:AddProcess(function()
            self.Animatior=self.parent:AddComponent(_G.CommonOHOH.load.CharacterAnimationCtrl()); 

            self.Animatior:PreLoadAnimationByRid(animationIdList,nil,nil,loop); 
        
        
            self.IdleState=self.character:GetState("idle")
            self.FollowState=self.character:GetState("follow")
        
            self.IdleState.EnterEvent:Connect(function()
                self:PlayIdle()
            end)
        
            self.FollowState.EnterEvent:Connect(function()
                self.Animatior:PlayAnimation("run")
            end)
        
            self.IdleState.ExitEvent:Connect(function()
                
                self:StopIdle()
            end)
        
            self.FollowState.ExitEvent:Connect(function()
                self.Animatior:StopAnimation("run")
            end)
    end)


end;

function M.PlayRun(self)
     self.current="run"
    self.Animatior:PlayAnimation(self.current)
   
end

function M.PlayIdle(self)

   self.idleCon= self:AddProcess(function()
        while true do
            task.wait()
            local t=1
             if math.random(1,100)<20 then
                self.Animatior:StopAnimation("idle2")
                 t=self.Animatior:PlayAnimation("idle1")
             else
                self.Animatior:StopAnimation("idle1")
                t=self.Animatior:PlayAnimation("idle2")
             end
             self:Wait(t+math.random(8,15))
        end
    end)
end

function M.StopIdle(self)
    if self.idleCon then
        self.idleCon:Disconnect()
        self.idleCon=nil
    end

    self.Animatior:StopAnimation("idle1")
    self.Animatior:StopAnimation("idle2")

end

function M.dtor(self)

    if self.idleCon then
        self.idleCon:Disconnect()
        self.idleCon=nil
    end
    self.Animatior:dtor()
	M.super.dtor(self);
end



return M